package com.sea.physical.service.impl;


import com.codecrane.core.util.CountryEnum;
import com.codecrane.core.web.BatchImportUtils;
import com.sea.operate.entity.Related;
import com.sea.operate.entity.RelatedSku;
import com.sea.operate.service.RelatedService;
import com.sea.operate.service.RelatedSkuService;
import com.sea.physical.entity.Cabinet;
import com.sea.physical.entity.CreatePlan;
import com.sea.physical.entity.CreateSku;
import com.sea.physical.entity.Transport;
import com.sea.physical.mapper.CreateSkuMapper;
import com.sea.physical.service.CabinetService;
import com.sea.physical.service.CreatePlanService;
import com.sea.physical.service.CreateSkuService;
import com.sea.physical.service.TransportService;
import com.sea.product.entity.Channel;
import com.sea.product.entity.Product;
import com.sea.product.mapper.ChannelMapper;
import com.sea.product.service.ChannelService;
import com.sea.product.service.ProductService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.List;

@Service
public class CreateSkuServiceImpl implements CreateSkuService {

    @Autowired
    private CreateSkuMapper createSkuMapper;
    @Autowired
    private ProductService productService;
    @Autowired
    private ChannelMapper channelMapper;
    @Autowired
    private RelatedSkuService relatedSkuService;
    @Autowired
    private RelatedService relatedService;
    @Autowired
    private CreatePlanService createPlanService;
    @Autowired
    private CabinetService cabinetService;
    @Autowired
    private TransportService transportService;

    @Override
    public List<CreateSku> findBySupplierQualityDetail(String createSupplierName) {
        return createSkuMapper.findBySupplierQualityDetail(createSupplierName);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importSku(InputStream inputStream, String fileName,Integer createId) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream,fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<CreateSku> skuList = BatchImportUtils.iteratorForCreateSku(sheet);
            if(null==skuList||skuList.size()==0){
                throw new Exception("请至少新增一条明细！");
            }else{
                for(CreateSku createSku:skuList){
                    Product product = productService.findBySku(createSku.getCreateSku());
                    if(null==product){
                        throw new RuntimeException("SKU【"+createSku.getCreateSku()+"】不存在!");
                    }
                    Channel channel = channelMapper.findByChannelName(createSku.getCreateChannelName());
                    if(null==channel){
                        throw new RuntimeException("渠道【"+createSku.getCreateChannelName()+"】不存在!");
                    }
                    RelatedSku relatedSku = relatedSkuService.findByChannelSku(createSku.getCreateChannelName(), createSku.getCreateSku());
                    if(null==relatedSku){
                        throw new RuntimeException("对照表不存在该信息【"+createSku.getCreateChannelName()+"-"+createSku.getCreateSku()+"】不存在!");
                    }else{
                        createSku.setCreateFnSku(relatedSku.getRelatedFnSku());
                    }

                    Related related = relatedService.findByAsin(relatedSku.getRelatedAsin(),createSku.getCreateChannelName());
                    if(null==related ){
                        throw new RuntimeException("暂未查询到Listing信息【"+createSku.getCreateChannelName()+"-"+createSku.getCreateSku()+"】!");
                    }else{
                        createSku.setCreateListing(related.getRelatedListing());
                    }
                    createSku.setCreateId(createId);
                    getCreateSkuInfo(createSku);
                    save(createSku);
                }
                createPlanService.updateTotal(createId);
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importCreatePlan(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream,fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<CreatePlan> skuList = BatchImportUtils.iteratorForCreatePlan(sheet);
            if(null==skuList||skuList.size()==0){
                throw new Exception("请至少新增一条明细！");
            }else{
                for(CreatePlan createPlan:skuList){
                    Cabinet cabinet = cabinetService.findByName(createPlan.getCreatePlanCabinetName());
                    if(null==cabinet){
                        throw new RuntimeException("柜型【"+createPlan.getCreatePlanCabinetName()+"】不存在!");
                    }
                    Transport transport = transportService.findByName(createPlan.getCreatePlanTransport());
                    if(null==transport){
                        throw new RuntimeException("运输方式【"+createPlan.getCreatePlanTransport()+"】不存在!");
                    }
                    CountryEnum countryEnum = CountryEnum.valueOf(createPlan.getCreatePlanCountry());
                    if(null==countryEnum){
                        throw new RuntimeException("国家【"+createPlan.getCreatePlanTransport()+"】不存在!");
                    }

                    CreatePlan createNo = createPlanService.findByCreateNo(createPlan.getCreatePlanNo());
                    if(null==createNo){
                        createPlanService.save(createPlan);
                    }else {
                        createPlan.setId(createNo.getId());
                        createPlanService.usave(createPlan);
                    }
                }

            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importCreateSku(InputStream inputStream, String fileName) throws Exception {
        Workbook workBook = BatchImportUtils.getWorkBook(inputStream,fileName);
        List<Sheet> sheets = BatchImportUtils.getSheets(workBook);
        if (null != sheets && sheets.size() > 0) {
            Sheet sheet = sheets.get(0);
            List<CreateSku> skuList = BatchImportUtils.iteratorForCreatePlanSku(sheet);
            if(null==skuList||skuList.size()==0){
                throw new Exception("请至少新增一条明细！");
            }else{
                for(CreateSku createSku:skuList){
                    Product product = productService.findBySku(createSku.getCreateSku());
                    CreatePlan createNo = createPlanService.findByCreateNo(createSku.getCreatePlanNo());
                    if(null==createNo){
                        throw new RuntimeException("柜号【"+createSku.getCreatePlanNo()+"】不存在!");
                    }
                    if(null==product){
                        throw new RuntimeException("SKU【"+createSku.getCreateSku()+"】不存在!");
                    }
                    Channel channel = channelMapper.findByChannelName(createSku.getCreateChannelName());
                    if(null==channel){
                        throw new RuntimeException("渠道【"+createSku.getCreateChannelName()+"】不存在!");
                    }
                    RelatedSku relatedSku = relatedSkuService.findByChannelSku(createSku.getCreateChannelName(), createSku.getCreateSku());
                    if(null==relatedSku){
                        throw new RuntimeException("对照表不存在该信息【"+createSku.getCreateChannelName()+"-"+createSku.getCreateSku()+"】不存在!");
                    }else{
                        createSku.setCreateFnSku(relatedSku.getRelatedFnSku());
                    }

                    Related related = relatedService.findByAsin(relatedSku.getRelatedAsin(),createSku.getCreateChannelName());
                    if(null==related ){
                        throw new RuntimeException("暂未查询到Listing信息【"+createSku.getCreateChannelName()+"-"+createSku.getCreateSku()+"】!");
                    }else{
                        createSku.setCreateListing(related.getRelatedListing());
                    }
                    System.out.println(createSku.getCreateWareName()+"----"+createSku.getCreatePlanNo());
                    createSku.setCreateId(createNo.getId());
                    getCreateSkuInfo(createSku);
                    save(createSku);
                    createPlanService.updateTotal(createNo.getId());
                }
//                createPlanService.updateTotal(createId);
            }
        }
    }

    @Override
    public int save(CreateSku createSku) {
        getCreateSkuInfo(createSku);
        return createSkuMapper.insert(createSku);
    }

    @Override
    public List<CreateSku> findByCreateId(Integer createId) {
        return createSkuMapper.findByCreateId(createId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int delete(Integer id,Integer createId) {

        createSkuMapper.delete(id);
        return createPlanService.updateTotal(createId);
    }

    @Override
    public CreateSku findById(Integer id) {
        return createSkuMapper.findById(id);
    }

    @Override
    public List<CreateSku> exportByCreateId(Integer[] idList) {
        return createSkuMapper.exportByCreateId(idList);
    }

    @Override
    public CreateSku findBySkuChannel(String channelName, String createSku, Integer createId) {
        return createSkuMapper.findBySkuChannel(channelName, createSku, createId);
    }

    @Override
    public int usave(CreateSku createSku) {
        return createSkuMapper.update(createSku);
    }

    @Override
    public Integer updateQualityStatus(Integer id) {
        return createSkuMapper.updateQualityStatus(id);
    }

    @Override
    public List<CreateSku> findBySupplierQuality(CreateSku createSku) {
        return createSkuMapper.findBySupplierQuality(createSku);
    }

    @Override
    public List<CreateSku> getReport() {
        return createSkuMapper.getReport();
    }

    @Override
    public List<CreateSku> findByShipmentIdByCreateId(Integer createId) {
        return createSkuMapper.findByShipmentIdByCreateId(createId);
    }

    @Override
    public List<CreateSku> exportDeclarationByCreateId(Integer createId) {
        return createSkuMapper.exportDeclarationByCreateId(createId);
    }


    void getCreateSkuInfo(CreateSku createSku){
        Product product = productService.findBySku(createSku.getCreateSku());
        Integer createNumber = createSku.getCreateNumber();
        if(null==product.getProductContainCount()){
            throw new RuntimeException("内含件数不能为空!SKU:"+createSku.getCreateSku());
        }
        createSku.setCreateNumberTotal(createNumber*product.getProductContainCount());
        createSku.setCreateBoxNumber(((double)createNumber/(double)createSku.getCreatePackage()));
        createSku.setCreateWeightTotal(createSku.getCreateBoxNumber() * createSku.getCreateWeight());
        //判断是否为混装
//        if(!createSku.getCreatePackage().equals(createSku.getCreateNumber())) {
            createSku.setCreateKgTotal(createNumber * product.getProductWeight());
//        }
        createSku.setCreateVolumeTotal(createSku.getCreateBoxNumber() * createSku.getCreateVolume());
    }
}
